# -*- coding: utf-8 -*-
# @Version: Python 3.9
# @Time    : 2023/1/9 17:09
# @Author  : chenyinhua
# @File    : log_handle.py
# @Software: PyCharm
# @Desc: 日志处理

# 标准库导入
import os
# 第三方库导入
from loguru import logger


def capture_logs(**kwargs):
    logger.remove(handler_id=None)  # 清除之前的设置
    level = kwargs.get("log_level", None)
    log_dir = kwargs.get("log_dir", "./")
    logger.info("""
                        _    _         _      _____         _
         __ _ _ __ (_)  / \\  _   _| |_ __|_   _|__  ___| |_
        / _` | "_ \\| | / _ \\| | | | __/ _ \\| |/ _ \\/ __| __|
       | (_| | |_) | |/ ___ \\ |_| | || (_) | |  __/\\__ \\ |_
        \\__,_| .__/|_/_/   \\_\\__,_|\\__\\___/|_|\\___||___/\\__|
             |_|
             Starting      ...     ...     ...
           """)
    if level:
        # 仅捕获指定级别日志 设置生成日志文件，utf-8编码，每天0点切割，zip压缩，保留3天，异步写入
        logger.add(sink=os.path.join(log_dir, "runtime_{time}.log"),
                   level=level.upper(),
                   rotation="00:00",
                   retention="3 days",
                   compression="zip",
                   encoding="utf-8",
                   enqueue=True,
                   format="{time:YYYY-MM-DD HH:mm:ss} {level} From {module}.{function} : {message}")
    else:
        # 捕获所有日志
        logger.add(
            sink=os.path.join(log_dir, "runtime_{time}_all.log"),
            enqueue=True,
            encoding="utf-8",
            retention="3 days",
            compression="zip",
            rotation="00:00",
            format="{time:YYYY-MM-DD HH:mm:ss} {level} From {module}.{function} : {message}",
        )
